package com.chinatelecom.clientservice.service.impl;

import java.io.Serializable;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.session.RowBounds;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.chinatelecom.clientservice.dao.dev.BaseRolesMapper;
import com.chinatelecom.clientservice.dao.dev.BaseUsersMapper;
import com.chinatelecom.clientservice.pojo.BaseRoles;
import com.chinatelecom.clientservice.pojo.BaseRolesExample;
import com.chinatelecom.clientservice.pojo.BaseUsers;
import com.chinatelecom.clientservice.pojo.BaseUsersExample;
import com.chinatelecom.clientservice.pojo.BaseUsersExample.Criteria;
import com.chinatelecom.clientservice.pojo.ExtGridReturn;
import com.chinatelecom.clientservice.pojo.ExtPager;
import com.chinatelecom.clientservice.service.UserService;

@Service
public class UserServiceImpl implements UserService {
	@Autowired
	private BaseUsersMapper baseUsersMapper;
	@Autowired
	private BaseRolesMapper baseRolesMapper;

	public ExtGridReturn selectUserInfo(ExtPager pager, String realName) {
		BaseUsersExample example = new BaseUsersExample();
		Criteria criteria = example.createCriteria();
		// 排序信息
		if (StringUtils.isNotBlank(pager.getDir()) && StringUtils.isNotBlank(pager.getSort())) {
			String sortName = "";
			if (StringUtils.equals(pager.getSort(), "id")) {
				sortName = "id";
			} else if (StringUtils.equals(pager.getSort(), "account")) {
				sortName = "account";
			} else if (StringUtils.equals(pager.getSort(), "password")) {
				sortName = "password";
			} else if (StringUtils.equals(pager.getSort(), "realName")) {
				sortName = "real_name";
			} else if (StringUtils.equals(pager.getSort(), "sex")) {
				sortName = "sex";
			} else if (StringUtils.equals(pager.getSort(), "email")) {
				sortName = "email";
			} else if (StringUtils.equals(pager.getSort(), "mobile")) {
				sortName = "mobile";
			} else if (StringUtils.equals(pager.getSort(), "officePhone")) {
				sortName = "office_phone";
			} else if (StringUtils.equals(pager.getSort(), "errorCount")) {
				sortName = "error_count";
			} else if (StringUtils.equals(pager.getSort(), "lastLoginTime")) {
				sortName = "last_login_time";
			} else if (StringUtils.equals(pager.getSort(), "lastLoginIp")) {
				sortName = "last_login_ip";
			} else if (StringUtils.equals(pager.getSort(), "remark")) {
				sortName = "remark";
			}
			example.setOrderByClause(sortName + " " + pager.getDir());
		}
		if (StringUtils.isNotBlank(realName)) {
			criteria.andRealNameLike("%" + realName + "%");
		}
		int count = baseUsersMapper.countByExample(example);
		List<BaseUsers> list = null;
		if (pager.getLimit() != null && pager.getStart() != null) {
			list = baseUsersMapper.selectByExample(example, new RowBounds(pager.getStart(), pager.getLimit()));
		} else {
			list = baseUsersMapper.selectByExample(example);
		}
		return new ExtGridReturn(count, list);
	}

	@Override
	public List<BaseRoles> selectRoleList() {
		return baseRolesMapper.selectByExample(null);
	}

	@Override
	public int saveUserInfo(BaseUsers user, List<String> roleIds) {
		try{
			int saveInfo = 0;
			if(StringUtils.isNoneBlank(user.getId())){
				//修改操作
				saveInfo = baseUsersMapper.updateByPrimaryKeySelective(user);
			}else{
				//新增
				BaseUsersExample example = new BaseUsersExample();
				Criteria criteria = example.createCriteria();
				criteria.andAccountEqualTo(user.getAccount());
				int checkAccount = baseUsersMapper.countByExample(example);
				if(checkAccount > 0){
					return -1;
				}
				saveInfo = baseUsersMapper.insert(user);
			}
			
			if(saveInfo > 0){
				Map<String,String> params = new HashMap<String,String>();
				params.put("userId", user.getId());
				baseUsersMapper.deleteRoleByUserId(user.getId());
				for(String roleId : roleIds){
					params.put("roleId", roleId);
					baseUsersMapper.insertUserRoleInfo(params);
				}
			}
		}catch(Exception e){
			return 1;
		}
		return 0;
	}

	@Override
	public List<BaseRoles> selectUserRoleInfo(String userId) throws Exception{
		List<String> roleIds = baseUsersMapper.selectRoleIdByUserId(userId);
		BaseRolesExample example = new BaseRolesExample();
		example.createCriteria().andRoleIdIn(roleIds);
		return baseRolesMapper.selectByExample(example);
	}

	@Override
	public BaseUsers selectUserInfoByUserId(String userId) throws Exception {
		return baseUsersMapper.selectByPrimaryKey(userId);
	}

}
